package cqie.cn.frame.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
//import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author 王林
 * @version 1.0
 * @date 2021-09-06 周一
 * @time 8:37
 * @description
 */
@Configuration
@EnableOpenApi
@EnableSwagger2
@EnableKnife4j
//@Slf4j
@Import(BeanValidatorPluginsConfiguration.class)
public class Swagger2Config {

    @Bean(value = "defaultApi2")
    public Docket defaultApi2(Environment environment) {

        //设置要显示的swagger环境:只在生产环境中使用，在发布环境中不适用！
        Profiles profiles = Profiles.of("dev", "test");

        //通过environment.acceptsProfiles判断是否启动处于设定的环境中
        boolean flag = environment.acceptsProfiles(profiles);

        Docket docket = new Docket(DocumentationType.OAS_30)
//                .host("https://www.baidu.com")
//                .consumes(Sets.newHashSet("application/json"))
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .groupName("通用权限管理模块")//配置分组
//                .enable(flag)//配置dev是否启动 true:启动 false:不启动
                .select()
                .apis(RequestHandlerSelectors.basePackage("cqie.cn.frame.controller"))
                .paths(PathSelectors.any())
                .build() //build 工厂模式
                .enable(flag);
        return docket;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("通用权限管理系统文档Api")
                .description("swagger2 API 文档")
                .termsOfServiceUrl("")
                .contact(new Contact("王林", "/doc.html", "2741933961@qq.com"))
                .version("1.0")
                .build();
    }
}

